package com.zhongyouyun.jyj.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhongyouyun.admin.model.UserModel;
import com.zhongyouyun.cqyc.pojo.NoticePojo;
import com.zhongyouyun.jyj.model.DistrictModel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongyouyun.jyj.pojo.DistrictPojo;
import com.zhongyouyun.other.model.PageRequest;
import com.zhongyouyun.other.model.Result;
import com.zhongyouyun.utils.ModelUtils;
import com.zhongyouyun.utils.QueryUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhongyouyun.jyj.service.DistrictService;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author: shenyi
 * @Date: 2023-11-17
 * @Description: 教育局地区
 */
@RestController
@RequestMapping("/jyj/jyj_district")
@Api(value = "教育局-后台管理地区controller", tags = {"教育局-后台管理地区接口"})
public class DistrictController {
    @Autowired
    private DistrictService service;

    @GetMapping("/getDistrictList")
    @ResponseBody
    @ApiOperation(value = "获取所有地区", notes = "获取所有地区")
    public Result getDistrictList() {
        List<DistrictModel> districtModels = service.selectList(new QueryWrapper<>());
        return Result.builder().message("成功").status(0).data(districtModels).build();
    }

    @PostMapping("/findPage")
    @ResponseBody
    @ApiOperation(value = "地区分页列表", notes = "地区分页列表")
    public IPage<DistrictPojo> findPage(@ApiParam(required = true, name = "地区查询器", value = "id:地区ID") @RequestBody PageRequest pageRequest) {
        QueryWrapper<NoticePojo> queryWrapper = new QueryWrapper();
        QueryUtils.fiterQuery(queryWrapper, pageRequest);
        Page page = new Page(pageRequest.getPageNum(), pageRequest.getPageSize());
        return service.selectPojoPage(page, queryWrapper);
    }


    @PostMapping("/saveOrUpate")
    @ResponseBody
    @ApiOperation(value = "修改或者新增", notes = "通过ID判断修改或者新增")
    public Result saveOrUpate(@RequestBody DistrictModel districtModel) {
        UserModel userModel = (UserModel) SecurityUtils.getSubject().getPrincipal(); // 获取当前登录用户
        if (ObjectUtil.isNotEmpty(districtModel.getId()) && ObjectUtil.isNotEmpty(districtModel.getId())) {
            userModel.setUpdateBy(userModel.getId().toString());
        } else {
            ModelUtils modelutils = new ModelUtils();
            modelutils.initModel(districtModel, userModel.getId().toString());
            districtModel.setDeptId(userModel.getDeptId());
        }
        Boolean flag = service.saveOrUpdate(districtModel);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("保存失败").success(false).status(1).build();
        }
    }

    @GetMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除", notes = "通过ID删除")
    public Result delete(@ApiParam(name = "地区ID", value = "id", required = true) @RequestParam(value = "id") String id) {
        Boolean flag = false;
        flag = service.removeById(Long.valueOf(id));
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("删除失败").success(false).status(1).build();
        }
    }

    @PostMapping("/batchDelete")
    @ResponseBody
    @ApiOperation(value = "批量删除", notes = "通过ID删除")
    public Result batchDelete(@RequestBody List<String> ids) {
        List<Long> list = new ArrayList<Long>();
        ids.forEach(id -> {
            list.add(Long.valueOf(id));
        });
        Boolean flag = service.removeByIds(list);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("删除失败").success(false).status(1).build();
        }
    }
}
